package net.adst.util;

import com.toedter.calendar.JDateChooser;
import net.adst.resources.VariableNames;
import org.apache.poi.hssf.usermodel.HSSFCell;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: alekseipot
 * Date: 12.11.12
 * Time: 9:28
 * To change this template use File | Settings | File Templates.
 */
public class MyUtils {
    public static boolean isDate(HSSFCell cell) {
        if (cell != null) {
            try {
                return cell.getDateCellValue() != null;
            } catch (IllegalStateException e) {
                return false;
            }
        }
        return false;

    }

    public static Integer getAbcNum(char chr) {
        String str = String.valueOf(chr);
        String[] abc = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
        for (int i = 0; i < abc.length; i++) {
            if (abc[i].equals(str)) {
                return i;
            }
        }
        return -1;
    }

    public static boolean checkDates(Date date) {
        Calendar dtFrom = (Calendar) ((JDateChooser) DataManager.getObject(VariableNames.CALENDAR_DATE_FROM)).getCalendar().clone();
        Calendar dtTo = (Calendar) ((JDateChooser) DataManager.getObject(VariableNames.CALENDAR_DATE_TO)).getCalendar().clone();
        return compareDate(date, dtFrom.getTime()) && compareDate(dtTo.getTime(), date);
    }

    /**
     * return true if(date>=dateFrom)
     */
    public static boolean compareDate(Date date, Date dtFrom) {
        String dateStr = new SimpleDateFormat("dd.MM.yyyy").format(date);
        String dateFrom = new SimpleDateFormat("dd.MM.yyyy").format(dtFrom);
        double dayFrom = Double.valueOf(dateFrom.substring(0, 2));
        double monthFrom = Double.valueOf(dateFrom.substring(3, 5));
        double yearFrom = Double.valueOf(dateFrom.substring(6, 10));
        double dayDate = Double.valueOf(dateStr.substring(0, 2));
        double monthDate = Double.valueOf(dateStr.substring(3, 5));
        double yearDate = Double.valueOf(dateStr.substring(6, 10));
        if (yearDate >= yearFrom) {
            if (yearDate == yearFrom) {
                if (monthDate >= monthFrom) {
                    if (monthDate == monthFrom) {
                        if (dayDate >= dayFrom) {
                            return true;
                        } else {
                            return false;
                        }
                    } else {
                        return true;
                    }
                } else {
                    return false;
                }
            } else {
                return true;
            }
        } else {
            return false;
        }
    }
}
